package com.ls.fw.data.impl.nosql.hbase.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 使用ThreadLocal以空间换时间解决SimpleDateFormat线程安全问题。
 * 
 * @author
 * 
 */
public class SynchronizdDateUtil {

    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String SOLR_DATE_FORMAT =  "yyyy-MM-dd'T'HH:mm:ss'Z'";
   
    private static ThreadLocal<?> date_threadLocal = new ThreadLocal<Object>() {  
        protected synchronized Object initialValue() {  
            return new SimpleDateFormat(DATE_FORMAT);  
        }  
    };  
    private static ThreadLocal<?> solrdate_threadLocal = new ThreadLocal<Object>() {  
        protected synchronized Object initialValue() {  
            return new SimpleDateFormat(SOLR_DATE_FORMAT);  
        }  
    };  
    public static DateFormat getDateFormat() {  
        return (DateFormat) date_threadLocal.get();  
    }  
  
    public static DateFormat getSolrDateFormat() {  
        return (DateFormat) solrdate_threadLocal.get();  
    }  
    
    public static Date parseDate(String textDate) throws ParseException {
        return getDateFormat().parse(textDate);
    }
    
    public static Date parseSolrDate(String textDate) throws ParseException {
        return getSolrDateFormat().parse(textDate);
    }
    
    public static String formatDate(String textDate) throws ParseException {
    	 return getDateFormat().format(parseDate(textDate));
    }
    
    public static String formatSolrDate(String textDate) throws ParseException {
        return getSolrDateFormat().format(parseSolrDate(textDate));
    }
    
    public static void main(String[] args) throws ParseException {
		System.out.println(SynchronizdDateUtil.getDateFormat().parse("2014-12-12 11:12:11"));
	}
}